import qs from 'qs'
import { message } from 'antd'
import axios from 'axios'
// import store from '@/store'
// import router from '@/router'

function errorMsg (error) {
  if (error.response) {
    const { data } = error.response
    message.error(data.message)
  } else {
    message.error('系统错误，请稍后重试~')
  }
}
function responseMsg (response) {
  const { data } = response
  const { code} = data
  if (code !== '200') {
    if (code === '401') {
      message.warning('会话已过期，请重新登陆')
      // store.commit('setUser', null)
      // router.replace({ name: 'login' })
    } else {
      message.warning(data.message || '服务繁忙')
      // message.warning('服务繁忙')
    }
  }
}
const baseURL = process.env.SITE_ENV === 'prod' ? '' : 'http://qa.api.maryya.freeradio.cn'
const createaxios = axios.create({
  retry: { retries: 3 },
  baseURL: baseURL
})
const createfile = axios.create({
  retry: { retries: 3 },
  baseURL: baseURL
})
createaxios.interceptors.request.use(config => {
  // const {
  //   state: { loggedUser }
  // } = store
  let loggedUser = localStorage.getItem('LOGGED_USER')
  if (loggedUser) {
    loggedUser = JSON.parse(loggedUser)
  }
  const params = qs.parse(config.data)
  const isJSON = !!params.data
  if (isJSON) {
    config.headers['Content-Type'] = 'application/json;charset=utf-8'
  }
  config.headers.common['Access-Token'] = loggedUser ? loggedUser.access_token : ''
  config.transformRequest = [
    data => {
      const datal = isJSON ? JSON.stringify(config.method === 'delete' ? data : data.data) : qs.stringify(data)
      return datal
    }
  ]
  return config
}, (error) => {
  errorMsg(error)
})
createfile.interceptors.request.use(config => {
  let loggedUser = localStorage.getItem('LOGGED_USER')
    if (loggedUser) {
      loggedUser = JSON.parse(loggedUser)
    }
  // const {
  //   state: { loggedUser }
  // } = store
  config.headers['Content-Type'] = 'multipart/form-data'
  config.headers.common['Access-Token'] = loggedUser ? loggedUser.access_token : ''
  return config
}, (error) => {
  errorMsg(error)
})
createaxios.interceptors.response.use((response) => {
  responseMsg(response)
  return response
}, (error) => {
  errorMsg(error)
  return error
})
createfile.interceptors.response.use((response) => {
  responseMsg(response)
  return response
}, (error) => {
  errorMsg(error)
  return error
})
export default {
  baseURL,
  
  administrator_get (id) {
    return createaxios.get(`/administrators/${id}`)
  },
  administrator_login (data) {
    return createaxios.post(`/administrators/session`, data)
  },
  advimg_getlist (params) {
    return createaxios.get('/PointMallAds', { params })
  },
  advimg_getdetails (_id) {
    return createaxios.get(`/PointMallAds/${_id}`)
  },
  advimg_add (_id, data) {
    return createaxios.post('/PointMallAds', data)
  },
  advimg_elt (_id, data) {
    return createaxios.put(`/PointMallAds/${_id}`, data)
  },
  advimg_remove (_id, data) {
    return createaxios.delete(`/PointMallAds/${_id}`, data)
  },
  advimg_batch (data) {
    return createaxios.post('/PointMallAds/batch', { data })
  },
  advimg_typemaps () {
    return createaxios.get('/PointMallAds/typeMaps')
  },
  file_uploadimg (data) {
    return createfile.post('/resources', data)
  },
  // 上传图片
  basic_resources (data) {
    return createaxios.post('/resources', { data })
  },
  // 获取(默认的)城市
  basic_findcity (params) {
    return createaxios.get('/storeAreas', { params })
  },
  classify_getlist (params) {
    return createaxios.get('/PointMallGoodsClass', { params })
  },
  classify_getdetails (_id) {
    return createaxios.get(`/PointMallGoodsClass/${_id}`)
  },
  classify_add (_id, data) {
    return createaxios.post('/PointMallGoodsClass', data)
  },
  classify_elt (_id, data) {
    return createaxios.put(`/PointMallGoodsClass/${_id}`, data)
  },
  classify_remove (_id, data) {
    return createaxios.delete(`/PointMallGoodsClass/${_id}`, data)
  },
  classify_batch (data) {
    return createaxios.post('/PointMallGoodsClass/batch', { data })
  },
  //
  commodity_getlist (params) {
    return createaxios.get('/PointMallGoods', { params })
  },
  commodity_getdetails (_id) {
    return createaxios.get(`/PointMallGoods/${_id}`)
  },
  commodity_add (_id, data) {
    return createaxios.post('/PointMallGoods', data)
  },
  // elt and 下架商品 and 上架
  commodity_elt (_id, data) {
    return createaxios.put(`/PointMallGoods/${_id}`, data)
  },
  commodity_remove (_id) {
    return createaxios.delete(`/PointMallGoods/${_id}`)
  },
  commodity_batch (data) {
    return createaxios.post('/PointMallGoods/batch', { data })
  },
  commodity_typemaps () {
    return createaxios.get('/PointMallGoods/typeMaps')
  },
  // 专属活动专区
  commodity_areamaps () {
    return createaxios.get('/PointMallGoods/activityAreaMaps')
  },
  // PointMallGoodsSku
  commodity_goodssku (data) {
    return createaxios.post('/PointMallGoodsSku', data)
  },
  // PointMallGoodsSku
  commodity_removegoodssku (_id) {
    return createaxios.delete(`/PointMallGoodsSku/${_id}`)
  },
  //
  home_getorderslist () {
    return createaxios.get('/PointMallOrders?status=PAID')
  },
  home_getGoodsClass (params) {
    return createaxios.get('/PointMallGoodsClass', { params })
  },
  home_getGoods (params) {
    return createaxios.get('/PointMallGoods', { params })
  },
  // 统计
  home_getStatics (params) {
    return createaxios.get('/PointMallStatics', { params })
  },
  log_list (params) {
    return createaxios.get('/logs/pointMall', { params })
  },
  log_detail (id) {
    return createaxios.get(`/logs/${id}`)
  },
  orde_getlist (params) {
    return createaxios.get('/PointMallOrders', { params })
  },
  orde_elt (_id, data) {
    return createaxios.put(`/PointMallOrders/${_id}`, data)
  }

}
